<?php 
namespace Home\Model;
use Think\Model;

class UsersModel extends Model{
	
	protected $trunTableName;
	protected $userDate;
	
	function __construct(){
		
		parent::__construct();
		$this->trueTableName = "myweb_users";
		
	}
	
	/*用户注册*/
	function toSignUp($data){
		
		if($data == null) return "注册信息传入错误!请重试!";
		
		$userid = $this->where("username = '$data[username]'")->getField("userid");
		if($userid != null) return "该用户名已被注册!请重试!";
		
		$status = $this->data($data)->filter('strip_tags')->add($data);
		
		if(!$status) return "注册信息写入错误!请重试!";
		
		return $this->toLogin($data);
		
	}
	
	/*用户登录*/
	function toLogin($data){
		
		$userData = $this->where("username = '$data[username]' and password = '$data[password]'")->find();
		
		
		if($userData == null) return "用户名或密码错误!请重试!";
		
		session(C('WEB_PRE').'user',$userData);
		
		return 10;
		
	}
	
	/*更改密码*/
	function rePassword($data){
		
		$userData = session(C('WEB_PRE').'user');
		
		if($userData['password'] != $data['old_pwd']) return "原密码错误!";
		
		$newData['password'] = $data['new_pwd'];
		
		$status = $this->data($newData)->where("userid = ".(int)$userData['userid'])->save();
		
		if(!$status) return "用户信息写入错误!";
		
		$userData = $this->where("userid = ".(int)$userData['userid'])->find();
		
		session(C('WEB_PRE')."user",$userData);
		
		return 10;
	}
	
	/*更改信息*/
	function reInfo($data){
		
		$userData = session(C('WEB_PRE').'user');
		
		$status = $this->data($data)->filter("strip_tags")->where("userid = ".(int)$userData[userid])->save();
		
		if(!$status) return "用户信息写入错误!";
		
		$userData = $this->where("userid = ".(int)$userData['userid'])->find();
		
		session(C('WEB_PRE')."user",$userData);
		
		return 10;
		
	}
	
}
